<?php

function appObHandler($str)
{
    $str = preg_replace('/\n(\s+\/\/.*)/', ' ', $str);
    $str = preg_replace('/[ \n\r]{1,}/', ' ', $str);

    $err = explode("<b>Fatal error</b>:", $str);
    if (count($err) > 1) {
        $errorType = "Fatal error";
        //        $errno = E_ERROR;
    } else {
        $err = explode('<b>Parse error</b>:', $str);
        if (count($err) > 1) {
            $errorType = "Parse error";
            //            $errno = E_PARSE;
        } else {
            return ($str);
        }
    }
    if (isset($errorType)) {
        $str = "<b>$errorType</b>:{$err[1]}";
    }

    return $str;
}

// Handle the output stream and set a handler function.
ob_start('appObHandler');

// error handler function
function appErrorHandler($errNo, $errStr, $errFile, $errLine)
{
    if (error_reporting() == 0) {
        return false;
    }

    switch ($errNo) {
        case E_USER_ERROR:
            $typeName = 'APP ERROR';
            break;

        case E_USER_WARNING:
            $typeName = 'APP WARNING';
            break;

        case E_USER_NOTICE:
            $typeName = 'APP NOTICE';
            break;

        case E_ERROR:
            $typeName = 'Error';
            break;

        case E_NOTICE:
            $typeName = 'Notice';
            break;

        case E_WARNING:
            $typeName = 'Warning';
            break;

        case E_CORE_ERROR:
            $typeName = 'Core Error';
            break;

        case E_CORE_WARNING:
            $typeName = 'Core Warning';
            break;

        case E_COMPILE_ERROR:
            $typeName = 'Compile Error';
            break;

        case E_COMPILE_WARNING:
            $typeName = 'Compile Warning';
            break;

        case E_STRICT:
            $typeName = 'Strict Notice';
            break;

        case E_RECOVERABLE_ERROR:
            $typeName = 'Recoverable Error';
            break;

        case E_DEPRECATED:
            $typeName = 'Deprecated';
            break;

        default:
            $typeName = "Unknown error";
            break;
    }
    $result = "<b>$typeName</b>: [$errNo] $errStr" . PHP_EOL .
              " on line <b>$errLine</b> in <b>$errFile</b>" .
              ""
    ;


    /*
     * Use only APP handler and logging
     */
    if (strtolower(@ $_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
        $result = strip_tags($result);
    } else {
        $result = "<pre>$result</pre>";
    }

    $e = new Rent_Exception($result);
    www($e->getMessage());
    www1($e->getTraceAsString());

    /* Don't execute PHP internal error handler */
    return true;

    /*
     * Use APP and PHP handler and logging
     */
    /* Do execute PHP internal error handler */
    //return false;
}

// Set an error handler for warnings and notices.
set_error_handler('appErrorHandler');




/************** TEST AREA **************/
// Generate a notice.
//echo $nonExistedVariable;
// Generate a fatal error.
//callToUndefinedFunction();
